﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace WindowsFormsApplication1
{
    public partial class admin : Form
    {
        private int state = 1;
        string name = "";
        public void setName(string name)
        {
            this.name = name;
        }
        public admin()
        {
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void admin_Load(object sender, EventArgs e)
        {
            comboBox1.Visible = false;
            load_kq_day(null);
            state = 2;
            //loadd2();
            load_combox();
            label1.Text = "您好，管理员 " + name + " ,您可以选择：";
            label2.Text = "考勤记录";
        }
        private void load_kq_day(string date)
        {
                
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from kq_table order by date DESC";
            if( date!=null)
                query = "select * from kq_table where date='"+date+"' order by date DESC";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            bindingSource1.DataSource = myds.Tables["Bill"];
            dataGridView1.DataSource = bindingSource1;
            dataGridView1.Columns["staffid"].HeaderText = "员工编号";
            dataGridView1.Columns["staffname"].HeaderText = "员工姓名";
            dataGridView1.Columns["date"].HeaderText = "考勤日期";
            conn.Close();
        }

        private void load_kq_month(string staffid)
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from kq_month_table order by month DESC";
            if(staffid!=null)
                query = "select * from kq_month_table where staffid='"+staffid+"' order by month DESC";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            bindingSource1.DataSource = myds.Tables["Bill"];
            dataGridView1.DataSource = bindingSource1;
            dataGridView1.Columns["year"].HeaderText = "年份";
            dataGridView1.Columns["month"].HeaderText = "月份";
            dataGridView1.Columns["staffid"].HeaderText = "员工编号";
            dataGridView1.Columns["staffname"].HeaderText = "员工姓名";
            dataGridView1.Columns["checkin_num"].HeaderText = "签到天数";
            dataGridView1.Columns["leave_num"].HeaderText = "请假天数";
            dataGridView1.Columns["tot_num"].HeaderText = "总计";
            conn.Close();
        }

        private void load_combox()
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from staff";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            //bindingSource3.DataSource = myds.Tables["Bill"].Rows.;
            //comboBox1.DataSource = bindingSource3;
            comboBox1.DataSource = myds.Tables[0];
            comboBox1.DisplayMember = myds.Tables[0].Columns[0].ColumnName;//显示的文本
            comboBox1.ValueMember = myds.Tables[0].Columns[1].ColumnName;//对应的值
            conn.Close();
        }
        private void load_qj(string staffid)
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from qj_tg_table order by qj_tg_date DESC";
            if(staffid!=null)
                query = "select * from qj_tg_table where staffid='"+staffid+"' order by qj_tg_date DESC";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            bindingSource1.DataSource = myds.Tables["Bill"];
            dataGridView1.DataSource = bindingSource1;
            dataGridView1.Columns["num"].HeaderText = "编号";
            dataGridView1.Columns["staffid"].HeaderText = "员工编号";
            dataGridView1.Columns["staffname"].HeaderText = "员工姓名";
            dataGridView1.Columns["leavedate"].HeaderText = "请假日期";
            dataGridView1.Columns["qjdate"].HeaderText = "申请日期";
            dataGridView1.Columns["qj_tg_date"].HeaderText = "申请通过日期";
            conn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label2.Text = "考勤记录";
            dateTimePicker1.Visible = true;
            comboBox1.Visible = false;
            load_kq_day( null);
            state = 2;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Visible = false;
            comboBox1.Visible = true;
            load_kq_month(null);
            state = 3;
            label2.Text = "月统计考勤记录";
        }

        private void button4_Click(object sender, EventArgs e)
        {//重置按钮
            if (state == 2)
                load_kq_day( null);
            if (state == 3)
                load_kq_month(null);
            if (state == 1)
                load_qj(null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string choosedate =
                dateTimePicker1.Value.Year.ToString() + "-" + dateTimePicker1.Value.Month.ToString() + "-" + dateTimePicker1.Value.Day.ToString();
            string staffid = comboBox1.SelectedValue.ToString();
            string staffname = comboBox1.GetItemText(comboBox1.Items[comboBox1.SelectedIndex]);
            if (state == 2)
            {
                load_kq_day(choosedate);
                label2.Text = "正在查看 " + choosedate + " 的考勤记录";
            }
            if (state == 3)
            {
                load_kq_month(staffid);
                label2.Text = "正在查看 " + staffname + " 的所有月份出勤记录";
            }
            if (state == 1)
            {
                load_qj(staffid);
                label2.Text = "正在查看 " + staffname + " 的所有请假记录";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Visible = false;
            comboBox1.Visible = true;
            load_qj(null);
            state = 1;
            label2.Text = "请假记录";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            PxAdmin pxadmin = new PxAdmin();
            pxadmin.Show();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            adminpj ap = new adminpj();
            ap.Show();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            release_task rt = new release_task();
            rt.Show();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            adminjx jx = new adminjx();
            jx.Show();
        }
    }
}
